#!/usr/bin/python

"""
    This file is part of benjap.

    Copyright Graham Jones 2010.

    Benjap is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    banjap is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with benjap (COPYING file).  
    If not, see <http://www.gnu.org/licenses/>.

    $Id: photoApp.py 38 2011-01-03 22:15:13Z grahamjones139 $
"""
"""
Photo Viewer application

"""
import os    

# Start pygame
from appBase import appBase
import pygame
from pygame.locals import QUIT, KEYDOWN, \
    K_ESCAPE, MOUSEBUTTONDOWN, MOUSEMOTION, K_q, K_F4
from random import random, randint, choice
import os

class photoApp(appBase):

    def __init__(self,parent):
    
        print "photoApp.__init__()"
        appBase.__init__(self,parent)

        musicFile = "sounds/popcorn.mid"

        pygame.mixer.music.load(musicFile)
        pygame.mixer.music.play(-1)

        photoDir = "photos"
        photoExt = ['png','jpg','jpeg']
        self.interval = 3   # seconds
        self.elapsedTime = 0. # seconds

        self.photoList = []
        fileList = os.listdir(photoDir)
        for fname in fileList:
            parts = fname.split('.')
            ext = parts[len(parts)-1].lower()
            if ext in photoExt:
                self.photoList.append("%s/%s" % (photoDir,fname))
        print self.photoList
        self.showPhoto()

    def onStop(self):
        print "photoApp.onStop()"
        pygame.mixer.music.stop()

    def processEvent(self,event):
        #print "starsApp.processEvent()"
        #if event.type == MOUSEBUTTONDOWN:
        #    if event.button == 1:
        #        self.setOrigin(event.pos)
        if event.type == MOUSEMOTION:
            pass

    def processDisplay(self,delay=0):
        self.elapsedTime += delay/1000.
        if self.elapsedTime > self.interval:
            self.showPhoto()
            self.elapsedTime = 0.0




    def showPhoto(self):
        img = pygame.image.load(choice(self.photoList))
        img = pygame.transform.smoothscale(img, self.screen_size).convert()
        self.screen.blit(img,(0,0))
        pygame.display.flip()


